package com.mj.activity;

import java.util.ArrayList;

import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.mj.R;
import com.mj.adapter.DetailPagerAdapter;
import com.mj.base.BaseActivity;
import com.mj.bean.Album;
import com.mj.bean.Avatar;
import com.mj.http.Json;
import com.mj.http.Url;

public class Activity_Detail extends BaseActivity {
	private int[] tv_ids = new int[] { R.id.textView1, R.id.textView2, R.id.textView3 };
	public ArrayList<Album> albums = new ArrayList<Album>();
	public Avatar avatar;
	private ViewPager viewPager;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_detail);
		avatar = (Avatar) getIntent().getSerializableExtra("avatar");
		// title
		aq.id(R.id.btn_left).clicked(this, "back");
		aq.id(R.id.btn_right).gone();
		aq.id(R.id.tv_title).text(avatar.getNickname());
		getData();
	}

	private void getData() {
		baseCallBack callBack = new baseCallBack() {
			@Override
			public void onSuccess(JSONObject object) {
				super.onSuccess(object);
				albums = Json.getAlbums(object);
				avatar = Json.getAvatar(object);
				initView();
			}
		};
		String url = Url.detail + "?id=" + avatar.getId();
		getJson(url, callBack, false);
	}

	private void initView() {
		viewPager = (ViewPager) findViewById(R.id.pager);
		viewPager.setAdapter(new DetailPagerAdapter(getSupportFragmentManager()));
		viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				for (int id : tv_ids) {
					aq.id(id).getView().setBackgroundResource(0);
				}
				aq.id(tv_ids[position]).getView().setBackgroundResource(R.drawable.title_bg_select);
			}
		});
	}

	/***
	 * 修改TAG选项
	 * 
	 * @param view
	 */
	public void change(View view) {
		int index = Integer.parseInt(view.getTag().toString());
		if (viewPager != null) {
			viewPager.setCurrentItem(index);
		}
	}

}
